package com.gepengjun.lims.controler;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gepengjun.lims.entity.Orgnazation;
import com.gepengjun.lims.entity.User;
import com.gepengjun.lims.service.OrgnazationService;
import com.gepengjun.lims.util.common.CommonUtil;
import com.gepengjun.lims.util.common.DataType;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jun
 * @since 2018-08-19
 */
@Controller
@RequestMapping("/lims/orgnazation")
public class OrgnazationController {

    protected static final org.slf4j.Logger logger = LoggerFactory.getLogger(OrgnazationController.class);

    @Autowired
    private OrgnazationService orgnazationService;

    @RequestMapping(value = "/initDeptList",method = RequestMethod.POST)
    @ResponseBody
    public List<Orgnazation> initDeptList(Orgnazation orgnazation){
        List<Orgnazation> orgnazationList = orgnazationService.initDeptList(orgnazation.getOrgId());
        return orgnazationList;
    }

    @GetMapping(value = "/orgList")
    public String orgList(Page<Orgnazation> page,Orgnazation orgnazation, ModelMap map){
        logger.info("orgnazationService--"+orgnazationService);
        page = orgnazationService.findOrgnazationPage(page,orgnazation);
        List<Orgnazation> orgnazationList = page.getRecords();
        orgnazationList = orgnazationService.initParent(orgnazationList);
        page.setRecords(orgnazationList);
        map.addAttribute("orgnazation",orgnazation);
        map.addAttribute("page",page);
        return "orgnazation/orgList";
    }

    @GetMapping(value = "/addOrgPage")
    public String addOrgPage(ModelMap map){
        List<Orgnazation> orgnazationList = orgnazationService.findAllAvailableOrgnazation();
        map.addAttribute("orgnazationList",orgnazationList);
        return "orgnazation/orgAdd";
    }

    @PostMapping(value = "/orgAdd")
    @ResponseBody
    public Boolean orgAdd(Orgnazation orgnazation){
        logger.info("orgAdd:--"+orgnazation);
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        orgnazation.setLastUpdUser(user.getUserId());
        orgnazation.setLastUpdTime(new Date());

        return orgnazationService.insert(orgnazation);
    }

    @GetMapping(value = "/findById")
    public String findById(Orgnazation orgnazation,ModelMap map){
        orgnazation = orgnazationService.selectById(orgnazation);
        orgnazation.setParent(orgnazationService.selectById(orgnazation.getParentId()));
        map.addAttribute("orgnazation",orgnazation);
        return "orgnazation/orgInfo";
    }

    @GetMapping(value = "/orgUpdPage")
    public String orgUpdPage(Orgnazation orgnazation,ModelMap map){
        orgnazation = orgnazationService.selectById(orgnazation);
        orgnazation.setParent(orgnazationService.selectById(orgnazation.getParentId()));
        List<Orgnazation> orgnazationList = orgnazationService.findAllAvailableOrgnazation();
        map.addAttribute("orgnazation",orgnazation);
        map.addAttribute("orgnazationList",orgnazationList);
        return "orgnazation/orgUpd";
    }

    @PostMapping(value = "/orgUpd")
    @ResponseBody
    public Boolean orgUpd(Orgnazation orgnazation){
        return orgnazationService.updateById(orgnazation);
    }

    @PostMapping(value = "/disableOrg")
    @ResponseBody
    public Boolean disableOrg(Orgnazation orgnazation){
        return orgnazationService.disableOrg(orgnazation);
    }

    @PostMapping(value = "/ableOrg")
    @ResponseBody
    public Boolean ableOrg(Orgnazation orgnazation){
        return orgnazationService.ableOrg(orgnazation);
    }

    @PostMapping("/batchDisableOrgs")
    @ResponseBody
    public Boolean batchDisableOrgs(String orgIds){
        if (!StringUtils.isEmpty(orgIds)){
            List<Long> orgIdList = (List<Long>) CommonUtil.covertArrayToList(orgIds.split(","), DataType.ISLONG);
            return orgnazationService.batchDisableOrgs(orgIdList);

        }
        return false;
    }

    @PostMapping("/batchAbleOrgs")
    @ResponseBody
    public Boolean batchAbleOrgs(String orgIds){
        if (!StringUtils.isEmpty(orgIds)){
            List<Long> orgIdList = (List<Long>) CommonUtil.covertArrayToList(orgIds.split(","), DataType.ISLONG);
            return orgnazationService.batchAbleOrgs(orgIdList);

        }
        return false;
    }
}

